package com.sunday.common.trace.skywalking.study.dubbo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolTaskExecutor threadPool() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("threadPool-");
        //核心线程数
        executor.setCorePoolSize(1);
        //最大线程数
        executor.setMaxPoolSize(1);
        //队列最大长度 >=mainExecutor.maxSize
        executor.setQueueCapacity(1);
        //线程池维护线程所允许的空闲时间
        executor.setKeepAliveSeconds(60);
        //线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
